package com.appspot.bs24event.service;

import java.util.List;
import java.util.Map;

import org.slim3.datastore.Datastore;
import org.slim3.util.BeanUtil;

import com.appspot.bs24event.meta.AttendanceMeta;
import com.appspot.bs24event.model.Attendance;
import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.Transaction;


public class AttendanceService {

    private AttendanceMeta meta = AttendanceMeta.get();

    public List<Attendance> getList(Key eventKey, Long eventVersion) {
        return Datastore.query(meta).filter(meta.eventKey.equal(eventKey), meta.eventVersion.equal(eventVersion)).asList();
    }

    public Attendance insert(Map<String, Object> input) {
        Attendance attendance = new Attendance();
        BeanUtil.copy(input, attendance);
        Transaction tx = Datastore.beginTransaction();
        Datastore.put(attendance);
        tx.commit();
        return attendance;
    }

    public void delete(Key key, Long version) {
        Transaction tx = Datastore.beginTransaction();
        Attendance attendance = Datastore.get(tx, meta, key, version);
        Datastore.delete(tx, attendance.getKey());
        tx.commit();
    }
    public int getCount(Key eventKey, Long eventVersion,String status){
        int count =  Datastore.query(meta).filter(meta.eventKey.equal(eventKey), meta.eventVersion.equal(eventVersion),meta.status.equal(status)).count();
        return count;
    }
}
